/************************************************************************************/
/*  Program name CAPFAIMP_RETIREMENTS.sql                                  			*/
/*  Creater:  NIP Karen (PCCW - UniHub)                                         	*/
/*************   History   *************************                                */
/* 16 JAN, 2006   NIP Karen         First created                               	*/
/************************************************************************************/
SET SERVEROUTPUT ON SIZE 1000000;

ALTER TRIGGER CSE_FA_RETIREMENTS_ARI DISABLE;

DECLARE
	lv_Prog_ID		VARCHAR2(100)	:=	'CAPFAIMP_RETIREMENTS';

	lv_Scc_Count	NUMBER	:=	0;
	lv_Err_Count	NUMBER	:=	0;
	lv_Err_Msg		VARCHAR2(500)	:=	'No Error';

	CURSOR Cur_F IS
	SELECT
		*
	FROM
		CRST_FA_RETIREMENTS;
	
	lv_Next_Seq_ID			NUMBER;
	lv_Txn_Header_ID_N_In	NUMBER;

BEGIN
	UPDATE
		FA_RETIREMENTS
	SET	
		BOOK_TYPE_CODE	=	CAPBRIMP_MAPPING.RenameStr(SUBSTR(BOOK_TYPE_CODE,1,4), BOOK_TYPE_CODE)
	WHERE BOOK_TYPE_CODE IN
		(
		SELECT 
			BOOK_TYPE_CODE
		FROM 
			CRST_FA_RETIREMENTS
		);
	
	FOR C1 IN Cur_F LOOP
		BEGIN		
			SELECT	FA_RETIREMENTS_S.NextVal
			INTO	lv_Next_Seq_ID
			FROM	DUAL;
	   
			SELECT	TRANSACTION_HEADER_ID_N
			INTO	lv_Txn_Header_ID_N_In
			FROM	CRST_FA_TXN_HEADERS_MAP
			WHERE	TRANSACTION_HEADER_ID	=	C1.TRANSACTION_HEADER_ID_IN;

			INSERT	INTO CRST_FA_RETIREMENTS_MAP VALUES
			(
			C1.RETIREMENT_ID,   
			C1.TRANSACTION_HEADER_ID_IN,
			lv_Next_Seq_ID,
			lv_Txn_Header_ID_N_In
			);
		
			INSERT INTO FA_RETIREMENTS
			VALUES
			(
			lv_Next_Seq_ID,
			C1.BOOK_TYPE_CODE,
			CAPBRIMP_MAPPING.GetAssetIDMapping(C1.ASSET_ID),
			CAPBRIMP_MAPPING.GetTXNHeaderIDMapping(C1.TRANSACTION_HEADER_ID_IN),
			C1.DATE_RETIRED,
			C1.DATE_EFFECTIVE,
			C1.COST_RETIRED,
			C1.STATUS,
			C1.LAST_UPDATE_DATE,
			CAPBRIMP_COMMON.GetUser(C1.LAST_UPDATED_NAME),
			C1.RETIREMENT_PRORATE_CONVENTION,
			CAPBRIMP_MAPPING.GetTXNHeaderIDMapping(C1.TRANSACTION_HEADER_ID_OUT),
			C1.UNITS,
			C1.COST_OF_REMOVAL,
			C1.NBV_RETIRED,
			C1.GAIN_LOSS_AMOUNT,
			C1.PROCEEDS_OF_SALE,
			C1.GAIN_LOSS_TYPE_CODE,
			C1.RETIREMENT_TYPE_CODE,
			C1.ITC_RECAPTURED,
			C1.ITC_RECAPTURE_ID,
			C1.REFERENCE_NUM,
			C1.SOLD_TO,
			C1.TRADE_IN_ASSET_ID,
			C1.STL_METHOD_CODE,
			C1.STL_LIFE_IN_MONTHS,
			C1.STL_DEPRN_AMOUNT,
			CAPBRIMP_COMMON.GetUser(C1.CREATED_NAME),
			C1.CREATION_DATE,
			C1.LAST_UPDATE_LOGIN,
			C1.ATTRIBUTE1,
			C1.ATTRIBUTE2,
			C1.ATTRIBUTE3,
			C1.ATTRIBUTE4,
			C1.ATTRIBUTE5,
			C1.ATTRIBUTE6,
			C1.ATTRIBUTE7,
			C1.ATTRIBUTE8,
			C1.ATTRIBUTE9,
			C1.ATTRIBUTE10,
			C1.ATTRIBUTE11,
			C1.ATTRIBUTE12,
			C1.ATTRIBUTE13,
			C1.ATTRIBUTE14,
			C1.ATTRIBUTE15,
			C1.ATTRIBUTE_CATEGORY_CODE,
			C1.REVAL_RESERVE_RETIRED,
			C1.UNREVALUED_COST_RETIRED,
			C1.BONUS_RESERVE_RETIRED
			);
		EXCEPTION
			WHEN OTHERS THEN
				DBMS_OUTPUT.Put_Line(SQLERRM);
				lv_Err_Count	:=	lv_Err_Count + 1;
		END;
		lv_Scc_Count	:=	lv_Scc_Count	+	1;
	END LOOP;
	
	IF	lv_Err_Count	>	0	THEN
		ROLLBACK;
	END IF;

	INSERT INTO CAPCNV_LOG(
		PROG_ID,
		PROCESS_DATE,
		RECORD_PROCESSED,
		RECORD_REJECTED,
		RECORD_EXTRACTED,
		ERR_MSG
	) VALUES (
		lv_Prog_ID,
		SYSDATE,
		lv_Scc_Count,
		lv_Err_Count,
		lv_Scc_Count-lv_Err_Count,
		lv_Err_Msg
	);
	
	COMMIT;
END;
/

ALTER TRIGGER CSE_FA_RETIREMENTS_ARI ENABLE;